/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
#include "glcd_ll.h"
#include "arm_comm.h"
#include "includes.h"

#define DLY_100US       450
#define LED1_MASK       GPIO_Pin_6
#define LED2_MASK       GPIO_Pin_7
#define LED3_MASK       GPIO_Pin_8
#define LED4_MASK       GPIO_Pin_9
#define LEDS_MASK       GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9
#define LEDS_PORT       GPIOF
#define length           4

extern FontType_t Terminal_6_8_6;
extern FontType_t Terminal_9_12_6;
extern FontType_t Terminal_18_24_12;

USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
Buttons_t ButtCtrl;

Int32U CriticalSecCntr;
volatile Boolean SysTickFl;
volatile Boolean Rx;
uint8_t character;
char menyplats[length];
int pek = 0;

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
void Rx_act(uint8_t);

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

//Gives a sign that clock has ticked.
Boolean TickHandler(void)
{
  SysTickFl = TRUE;
  return SysTickFl;
}

Boolean RxHandler(void)
{
  character = (USART_ReceiveData(USART1) & 0x7F);
  Rx = TRUE;
  return Rx;
}


void DelayResolution100us(Int32U Dly)
{
  for(; Dly; Dly--)
  {
    for(volatile Int32U j = DLY_100US; j; j--)
    {
    }
  }
}

//Main program  
void main(void)
{
  Boolean nyTryck[3];
  for(int i = 0; i < 3; i++)
    nyTryck[i] = TRUE;
  //Configurations
  RCC_Configuration();    
  NVIC_Configuration();
  GPIO_Configuration();  
  USART_Configuration();
  
  // Systick configurations
  SysTick_Config(9000000);
  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
  
  // GLCD init
  GLCD_PowerUpInit((pInt8U)Egen_Logo.pPicStream);
  GLCD_Backlight(BACKLIGHT_ON);
  GLCD_SetFont(&Terminal_9_12_6,0x000F00,0x00FF0);
  GLCD_SetWindow(10,10,131,131);
  
   // Init motor
   // initMotor();
   // motorEnable();
  
  // I2C2 init
  I2C2_Init();
  
  // Buttons init
  ButtonsInit();
  
  // Init Temperature sensor
  if(   FALSE == TempSensor_Init()
     || FALSE == TempSensorShutdown(FALSE)
     || FALSE == TempSensor_Conf(31.0,30.0,TEMP_SENOSR_COMP_MODE,2))
  {
    // Initialization fault
    printf("Did not work to initialize Temperature sensor");
    while(1);
  }
  
  GLCD_TextSetPos(0,0);
  GLCD_print("->");
  GLCD_TextSetPos(2,0);
  GLCD_print("On/Off LED1");
  GLCD_TextSetPos(2,1);
  GLCD_print("On/Off LED2");
  GLCD_TextSetPos(2,2);
  GLCD_print("On/Off LED3");
  GLCD_TextSetPos(2,3);
  GLCD_print("On/Off LED4");
  GLCD_TextSetPos(2,4);
  GLCD_print("Check temperature");
   
  
  while(1)
  {
    if(Rx)
    {
      Rx = FALSE;
      Rx_act(character); 
    }
    ButtCtrl = GetButtons();
    if(ButtCtrl.JsDown && nyTryck[0])
    {
      nyTryck[0] = FALSE;
      Delay(2500);
      GLCD_TextSetPos(0,pek);
      GLCD_print("  ");
      pek++;
      if(pek >= 5)
        pek = 0;
      GLCD_TextSetPos(0,pek);
      GLCD_print("->"); 
    }
    else if(!ButtCtrl.JsDown)
      nyTryck[0] = TRUE;
    
    if(ButtCtrl.JsUp && nyTryck[1])
    {
      Delay(2500);
      nyTryck[1] = FALSE;
      GLCD_TextSetPos(0,pek);
      GLCD_print("  ");
      pek--;
      if(pek < 0)
        pek = 4;
      GLCD_TextSetPos(0,pek);
      GLCD_print("->"); 
    }
    else if(!ButtCtrl.JsDown)
      nyTryck[1] = TRUE;
    
    if(ButtCtrl.B2 && nyTryck[2])
    {
      nyTryck[2] = FALSE;
     ledxOnOff(pek);
    }
    else if(!ButtCtrl.B2)
      nyTryck[2] = TRUE;

  }
}


void RCC_Configuration(void)
{
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
     initialize the PLL and update the SystemFrequency variable. */
  SystemInit();
  
  RCC_APB2PeriphClockCmd(  RCC_APB2Periph_USART1, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
  
  // GPIO enable clock and release Reset
  RCC_APB2PeriphResetCmd(  RCC_APB2Periph_GPIOF, DISABLE);
  RCC_APB2PeriphClockCmd(  RCC_APB2Periph_GPIOF, ENABLE);

}


void GPIO_Configuration(void)
{   
  /* Configure USART Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
  
  /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  //Status LED port init
  GPIO_InitStructure.GPIO_Pin =  LEDS_MASK;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(LEDS_PORT, &GPIO_InitStructure);
  GPIO_WriteBit(LEDS_PORT,LED1_MASK,Bit_RESET);
  GPIO_WriteBit(LEDS_PORT,LED2_MASK,Bit_RESET);
  GPIO_WriteBit(LEDS_PORT,LED3_MASK,Bit_RESET);
  GPIO_WriteBit(LEDS_PORT,LED4_MASK,Bit_RESET);
}

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Enable the SysTick Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = SysTick_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);  
}

void USART_Configuration(void)
{
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  
  USART_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}

PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(EVAL_COM1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  {}

  return ch;
}
